package huixue.cn.com.rxapp.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import huixue.cn.com.library.util.LogUtils;
import huixue.cn.com.rxapp.R;

/**
 * Created by Administrator on 2018/8/19.
 */

public class RatioView extends FrameLayout {

    private float wh_ratio;

    public RatioView(Context context) {
        super(context);
    }

    public RatioView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //获取属性值
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RatioLayout);
        wh_ratio = typedArray.getFloat(R.styleable.RatioLayout_ratio,1.0f);

        LogUtils.e("wh_ratio=="+wh_ratio);
        typedArray.recycle();
    }

    public RatioView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取测量模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        // 父容器传过来的宽度的值
        int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
        LogUtils.e("width=="+width);

        int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingBottom() - getPaddingTop();

        height = (int) (width /wh_ratio + 0.5f);
        LogUtils.e("height=="+height);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public float getWh_ratio() {
        return wh_ratio;
    }

    public void setWh_ratio(float wh_ratio) {
        this.wh_ratio = wh_ratio;
    }
}
